package com.vancouver.knows.ui.fragment.citydiscount;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AlertDialog;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.everything.android.ui.overscroll.IOverScrollDecor;
import me.everything.android.ui.overscroll.IOverScrollState;
import me.everything.android.ui.overscroll.IOverScrollStateListener;
import me.everything.android.ui.overscroll.IOverScrollUpdateListener;

import com.github.ppamorim.dragger.DraggerPosition;
import com.pangu.service.ActionException;
import com.pangu.task.IResultReceiver;
import com.pangu.task.mark.ATaskMark;
import com.pangu.ui.refresh.ARefreshViewController;
import com.pangu.util.StringUtil;
import com.squareup.picasso.Picasso;
import com.vancouver.knows.MainApplication;
import com.vancouver.knows.R;
import com.vancouver.knows.business.bean.BusinessImageInfo;
import com.vancouver.knows.ui.activity.ImageGalleryActivity;
import com.vancouver.knows.ui.activity.WebViewActivity;
import com.vancouver.knows.ui.activity.citydiscount.BusinessImageViewerActivity;
import com.vancouver.knows.ui.activity.citydiscount.CityDiscountDetailActivity;
import com.vancouver.knows.ui.activity.citynews.CityNewsCommentsActivity;
import com.vancouver.knows.ui.activity.login.LoginActivity;
import com.vancouver.knows.ui.activity.userpost.CreateUserPostActivity;
import com.vancouver.knows.ui.fragment.citynews.CommentItemView;
import com.vancouver.knows.util.AppUtils;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.Constants;
import com.vancouver.knows.business.bean.CityDiscountDetailInfo;
import com.vancouver.knows.business.bean.UserInfo;
import com.vancouver.knows.business.task.mark.UserRateTaskMark;

import java.util.List;

public class CityDiscountPostMainDetailView implements IResultReceiver, IOverScrollUpdateListener, IOverScrollStateListener {

	@Bind(R.id.name)
	TextView name;
	@Bind(R.id.location)
	TextView location;
	@Bind(R.id.ratingBar)
	RatingBar ratingBar;
	@Bind(R.id.averageRate)
	TextView averageRate;
	@Bind(R.id.rateCount)
	TextView rateCount;
	@Bind(R.id.fiveRatingText)
	TextView fiveRatingText;
	@Bind(R.id.fourRatingText)
	TextView fourRatingText;
	@Bind(R.id.threeRatingText)
	TextView threeRatingText;
	@Bind(R.id.twoRatingText)
	TextView twoRatingText;
	@Bind(R.id.oneRatingText)
	TextView oneRatingText;
	@Bind(R.id.userRatingBar)
	RatingBar userRatingBar;
	@Bind(R.id.userRateText)
	TextView userRateText;
	@Bind(R.id.description)
	TextView description;
	@Bind(R.id.description_divider)
	View descriptionDivider;
	@Bind(R.id.description_expand_button)
	TextView descriptionExpandButton;
	@Bind(R.id.officialWebsite)
	TextView officialWebsite;
	@Bind(R.id.flod)
	TextView flod;
	@Bind(R.id.eventBannerLayout)
	View bannerLayout;
	@Bind(R.id.hotCommentLayout)
	LinearLayout hotCommentLayout;
	@Bind(R.id.suggestedBusinessLayout)
	LinearLayout suggestedBusinessLayout;
	@Bind(R.id.detailInfo)
	LinearLayout detailInfo;
	@Bind(R.id.promotionsLayout)
	LinearLayout promotionsLayout;
	@Bind(R.id.pro)
	TextView pro;
	@Bind(R.id.navigation)
	TextView navigation;
	@Bind(R.id.detail)
	TextView detail;
	@Bind(R.id.photoTitle)
	TextView photoTitle;
	@Bind(R.id.first_image)
	ImageView firstUserImage;
	@Bind(R.id.second_image)
	ImageView secondUserImage;
	@Bind(R.id.more_image_container)
	RelativeLayout moreImageContainer;
	@Bind(R.id.more_image)
	ImageView moreUserImage;
	@Bind(R.id.more_image_text)
	TextView moreUserImageText;
	@Bind(R.id.detail_reveal)
	RelativeLayout detailRevealLayout;

	private static final float OVERSCROLL_THRESHOLD = 200f;
	private boolean reachedOverScrollThreshold = false;
	private int scrollState = -1;
	private DraggerPosition dragPosition = DraggerPosition.TOP;

	private CityDiscountDetailActivity activity;
	private CityDiscountPullDetailControllerView rootView;
	private CityDiscountDetailInfo cityDiscountDetailInfo;
	private CommentItemView commentItemView;
	private BannerViewControll bannerViewControll;
	private PromotionsView promotionsView;
	private CityDiscountsView cityDiscountsView;
	private String qrCodeBitmapString = null;

	public CityDiscountPostMainDetailView(CityDiscountDetailActivity activity) {
		this.activity = activity;
		this.cityDiscountDetailInfo = activity.getInfo();
		rootView = new CityDiscountPullDetailControllerView(activity);
		rootView.init(cityDiscountDetailInfo.getId(), activity);
		ButterKnife.bind(this, rootView);
		init();
	}

	private void init() {
		if("Y".equals(cityDiscountDetailInfo.getPaid())){
			Drawable drawable = activity.getResources().getDrawable(R.drawable.image_detail);
			drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
			detail.setCompoundDrawables(null,drawable,null,null);
			detail.setTextColor(activity.getResources().getColor(R.color.common_font));

			pro.setTextColor(activity.getResources().getColor(R.color.common_font));
			pro.setText(Html.fromHtml(activity.getResources().getString(R.string.pro_text)));
			Drawable proDrawable = activity.getResources().getDrawable(R.drawable.pro_checked);
			proDrawable.setBounds(0, 0, proDrawable.getMinimumWidth(), proDrawable.getMinimumHeight());
			pro.setCompoundDrawables(proDrawable,null,null,null);
			rootView.setOverScroll(this);
		}else{
			Drawable drawable = activity.getResources().getDrawable(R.drawable.image_detail_dark);//details_popup
			drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
			detail.setCompoundDrawables(null,drawable,null,null);
			detail.setTextColor(activity.getResources().getColor(R.color.detail_gray));

			pro.setTextColor(activity.getResources().getColor(R.color.orange));
			pro.setText(R.string.erchant_promotion);
			pro.setCompoundDrawables(null,null,null,null);
			detailRevealLayout.setVisibility(View.GONE);
		}

		if ((cityDiscountDetailInfo.getLat() == null && cityDiscountDetailInfo.getLng() == null) ||
				cityDiscountDetailInfo.getLat().equals("0") && cityDiscountDetailInfo.getLng().equals("0")) {
			Drawable drawable = activity.getResources().getDrawable(R.drawable.dao_hang_dark);
			drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
			drawable.setColorFilter(activity.getResources().getColor(R.color.detail_gray), PorterDuff.Mode.SRC_ATOP);
			navigation.setCompoundDrawables(null,drawable,null,null);
			navigation.setTextColor(activity.getResources().getColor(R.color.detail_gray));
		} else {
			Drawable drawable = activity.getResources().getDrawable(R.drawable.dao_hang);
			drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
			drawable.clearColorFilter();
			navigation.setCompoundDrawables(null,drawable,null,null);
			navigation.setTextColor(activity.getResources().getColor(R.color.common_font));
		}

		if (StringUtil.isEmptyString(cityDiscountDetailInfo.getWebsite())) {
			officialWebsite.setVisibility(View.GONE);
		}

		if (cityDiscountDetailInfo.getCommentInfoList() == null
				|| cityDiscountDetailInfo.getCommentInfoList().size() == 0) {
			hotCommentLayout.setVisibility(View.GONE);
		} else {
			hotCommentLayout.setVisibility(View.VISIBLE);
			commentItemView = new CommentItemView();
			commentItemView.init(activity, hotCommentLayout, cityDiscountDetailInfo.getCommentInfoList(),
					cityDiscountDetailInfo.getId());
			if (cityDiscountDetailInfo.getCommentCount() > 4) {
				View buttonContainer = LayoutInflater.from(activity).inflate(R.layout.view_all_comments_button, null);
				Button viewAllComments = (Button) buttonContainer.findViewById(R.id.button);
				viewAllComments.setText(activity.getString(R.string.view_all_comments, cityDiscountDetailInfo.getCommentCount()));
				viewAllComments.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						Intent intent = new Intent(activity, CityNewsCommentsActivity.class);
						intent.putExtra("postId", cityDiscountDetailInfo.getId());
						activity.startActivity(intent);
					}
				});
				hotCommentLayout.addView(buttonContainer);
			}
		}

		if (cityDiscountDetailInfo.getBusinessInfoList() == null || cityDiscountDetailInfo.getBusinessInfoList().size() == 0) {
			suggestedBusinessLayout.setVisibility(View.GONE);
		} else {
			suggestedBusinessLayout.setVisibility(View.VISIBLE);
			cityDiscountsView = new CityDiscountsView(activity, cityDiscountDetailInfo.getBusinessInfoList());
			if (suggestedBusinessLayout.getChildCount() == 2) {
				suggestedBusinessLayout.removeViewAt(1);
			}
			suggestedBusinessLayout.addView(cityDiscountsView);
		}

		name.setText(cityDiscountDetailInfo.getName());

		if (cityDiscountDetailInfo.getAddress() != null && cityDiscountDetailInfo.getCity() != null) {
			String lookLoaction = activity.getString(R.string.look_location);
			String locationStr = cityDiscountDetailInfo.getAddress() + "," + cityDiscountDetailInfo.getCity() + "  "
					+ lookLoaction;
			location.setText(AppUtils.replaceColor(locationStr, lookLoaction,
					activity.getResources().getColor(R.color.orange)));
		} else {
			location.setVisibility(View.GONE);
		}

		updateUserRateView();
		userRatingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

			@Override
			public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
				if (!fromUser) {
					return;
				}

				if (!AppUtils.getLoginStatus(activity)) {
					updateUserRateView();
					return;
				}

				if (rating == 0) {
					userRateText.setText(activity.getString(R.string.user_rate_text_empty));

				} else {
					userRateText.setText(activity.getString(R.string.user_rate_text, rating + ""));
				}

				BusinessModule bModule = ((MainApplication) activity.getApplication()).getBusinessModule();
				UserInfo userInfo = bModule.getCacheManager().getUserInfo();
				UserRateTaskMark aTaskMark = bModule.getTaskMarkPool().createUserRateTaskMark(
						cityDiscountDetailInfo.getId(), rating);
				bModule.getServiceWrapper().userRateBusiness(CityDiscountPostMainDetailView.this, aTaskMark,
						cityDiscountDetailInfo.getId(), userInfo.getUserID(), rating);
			}
		});

		description.setText(cityDiscountDetailInfo.getDescription());
		description.post(new Runnable() {
            @Override
            public void run() {
                if (description.getLineCount() > 8) {
					description.setMaxLines(8);
					description.setEllipsize(TextUtils.TruncateAt.END);
					description.setText(cityDiscountDetailInfo.getDescription());
                    descriptionExpandButton.setVisibility(View.VISIBLE);
                } else {
                    descriptionExpandButton.setVisibility(View.GONE);
					descriptionDivider.setVisibility(View.GONE);
                }
            }
        });

		flod.setTag(false);

		promotionsView = new PromotionsView(activity);
		qrCodeBitmapString = AppUtils.createQrImage(activity);
		promotionsView.init(qrCodeBitmapString, promotionsLayout, cityDiscountDetailInfo);

		List<BusinessImageInfo> businessImageInfoList = cityDiscountDetailInfo.getBusinessUserImages();
		setUserImages(businessImageInfoList);

		if (bannerViewControll == null) {
			bannerViewControll = new BannerViewControll(activity, bannerLayout, this);
		}
		bannerViewControll.updateView(cityDiscountDetailInfo.getImages());
		if ("Y".equals(cityDiscountDetailInfo.getPaid())) {
			bannerViewControll.setOverScroll(this);
		}
	}

	public void setUserImages(List<BusinessImageInfo> businessImageInfoList) {

		if (businessImageInfoList == null || businessImageInfoList.size() == 0) {
			photoTitle.setText(activity.getString(R.string.business_photo_title, 0));
		} else {
			int size = businessImageInfoList.size();
			photoTitle.setText(activity.getString(R.string.business_photo_title, size));
			if (size > 0) {
				Picasso.with(activity).load(businessImageInfoList.get(0).getImageUrl()).fit().centerCrop().into(firstUserImage);
				firstUserImage.setVisibility(View.VISIBLE);
			}

			if (size > 1) {
				Picasso.with(activity).load(businessImageInfoList.get(1).getImageUrl()).fit().centerCrop().into(secondUserImage);
				secondUserImage.setVisibility(View.VISIBLE);
			}

			if (size > 2) {
				Picasso.with(activity).load(businessImageInfoList.get(2).getImageUrl()).fit().centerCrop().into(moreUserImage);
				moreUserImageText.setText(size - 2 + "+");
				moreImageContainer.setVisibility(View.VISIBLE);
				moreImageContainer.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						Intent intent = new Intent(activity, ImageGalleryActivity.class);
						intent.putExtra(ImageGalleryActivity.POST_ID_EXTRA, cityDiscountDetailInfo.getId());
						activity.startActivity(intent);
					}
				});
			}
		}
	}

	private void updateUserRateView() {
		userRatingBar.setRating((float) cityDiscountDetailInfo.getUserRating());
		if (cityDiscountDetailInfo.getUserRating() == 0) {
			userRateText.setText(activity.getString(R.string.user_rate_text_empty));

		} else {
			userRateText.setText(activity.getString(R.string.user_rate_text, cityDiscountDetailInfo.getUserRating()
					+ ""));
		}
		ratingBar.setRating((float) cityDiscountDetailInfo.getBusinessRating());
		averageRate.setText(activity.getString(R.string.average_rate, cityDiscountDetailInfo.getBusinessRating() + ""));
		rateCount.setText(activity.getString(R.string.rate_count, cityDiscountDetailInfo.getRatingCount()));
		fiveRatingText.setText(activity.getString(R.string.rate_text, cityDiscountDetailInfo.getRatingDetails()
				.getFiveStar()));
		fourRatingText.setText(activity.getString(R.string.rate_text, cityDiscountDetailInfo.getRatingDetails()
				.getFourStar()));
		threeRatingText.setText(activity.getString(R.string.rate_text, cityDiscountDetailInfo.getRatingDetails()
				.getThreeStar()));
		twoRatingText.setText(activity.getString(R.string.rate_text, cityDiscountDetailInfo.getRatingDetails()
				.getTwoStar()));
		oneRatingText.setText(activity.getString(R.string.rate_text, cityDiscountDetailInfo.getRatingDetails()
				.getOneStar()));
	}

	public void updatePromotionsView() {
		promotionsView.updateLayout();
	}

	public CityDiscountPullDetailControllerView getRootView() {
		return rootView;
	}

	@OnClick(R.id.location)
	public void onClickLocation() {
		showMap();
	}

	@OnClick(R.id.flod)
	public void onFlodClick(View view) {
		boolean flag = (boolean) view.getTag();
		if (flag) {
			view.setTag(false);
			((TextView) view).setText(R.string.flod_open);
			Drawable drawable = activity.getResources().getDrawable(R.drawable.ping_fen);
			int width = drawable.getMinimumWidth();
			int height = drawable.getMinimumHeight();
			drawable.setBounds(0, 0, width, height);
			((TextView) view).setCompoundDrawables(null, drawable, null, null);
			((TextView) view).setCompoundDrawablePadding(AppUtils.dip2px(activity,8)) ;
			detailInfo.setVisibility(View.GONE);
		} else {
			((TextView) view).setText(R.string.flod_close);
			Drawable drawable = activity.getResources().getDrawable(R.drawable.flod_close);
			drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
			((TextView) view).setCompoundDrawables(null, drawable, null, null);
			((TextView) view).setCompoundDrawablePadding(AppUtils.dip2px(activity,8)) ;
			view.setTag(true);
			detailInfo.setVisibility(View.VISIBLE);
		}
	}

	@OnClick(R.id.officialWebsite)
	public void onCitynewsSourceClick() {
		if (StringUtil.isEmptyString(cityDiscountDetailInfo.getWebsite())) {
			return;
		}

		AppUtils.showBrower(activity, cityDiscountDetailInfo.getWebsite());
	}

	@OnClick(R.id.phone)
	public void onPhoneClick() {
		if (StringUtil.isEmptyString(cityDiscountDetailInfo.getPhone())) {
			return;
		}

		AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.style.MyAlertDialogStyle);
		builder.setTitle("");
		builder.setMessage(cityDiscountDetailInfo.getPhone());
		builder.setPositiveButton(R.string.back, null);
		builder.setNegativeButton(R.string.go_phone, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + cityDiscountDetailInfo.getPhone()));
				activity.startActivity(intent);
			}
		});
		builder.show();
	}

	@OnClick(R.id.navigation)
	public void onNavigationClick() {

		if ((cityDiscountDetailInfo.getLat() == null && cityDiscountDetailInfo.getLng() == null) ||
				cityDiscountDetailInfo.getLat().equals("0") && cityDiscountDetailInfo.getLng().equals("0")) {
			return;
		}

		showMap();
	}

	@OnClick(R.id.description_expand_button)
	public void onDescriptionExpandClick() {

		if (TextViewCompat.getMaxLines(description) == 8) {
			description.setMaxLines(Integer.MAX_VALUE);
			description.setEllipsize(null);
			Drawable drawable = ContextCompat.getDrawable(activity, R.drawable.icon_arrow_up);
			drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
			descriptionExpandButton.setCompoundDrawables(null, null, drawable, null);
			descriptionExpandButton.setCompoundDrawablePadding(AppUtils.dip2px(activity, 8)) ;
		} else {
			description.setMaxLines(8);
			description.setEllipsize(TextUtils.TruncateAt.END);
			Drawable drawable = ContextCompat.getDrawable(activity, R.drawable.icon_arrow_down);
			drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
			descriptionExpandButton.setCompoundDrawables(null, null, drawable, null);
			descriptionExpandButton.setCompoundDrawablePadding(AppUtils.dip2px(activity, 8)) ;
		}

		description.setText(cityDiscountDetailInfo.getDescription());
	}

	@OnClick(R.id.add_image_button)
	public void onAddImageClick() {

		BusinessModule bModule = ((MainApplication) activity.getApplication()).getBusinessModule();
		UserInfo userInfo = bModule.getCacheManager().getUserInfo();
		if (userInfo == null || StringUtil.isEmptyString(userInfo.getUserID())) {
			activity.startActivity(new Intent(activity, LoginActivity.class));
			return;
		}

		Intent intent = new Intent(activity, CreateUserPostActivity.class);
		intent.putExtra(CreateUserPostActivity.ADD_PHOTOS_MODE_EXTRA, true);
		intent.putExtra(CreateUserPostActivity.POST_ID_EXTRA, cityDiscountDetailInfo.getId());
		activity.startActivityForResult(intent, CreateUserPostActivity.ADD_PHOTOS_REQUEST);
	}

	@OnClick({R.id.first_image, R.id.second_image})
	public void onViewImageClicked(View view) {

		Intent intent = new Intent(activity, BusinessImageViewerActivity.class);
		if (view.getId() == R.id.first_image) {
			intent.putExtra(BusinessImageViewerActivity.INDEX_EXTRA, 0);
		} else {
			intent.putExtra(BusinessImageViewerActivity.INDEX_EXTRA, 1);
		}

		intent.putExtra(BusinessImageViewerActivity.POST_ID_EXTRA, cityDiscountDetailInfo.getId());
		activity.startActivity(intent);
	}

	private void showMap(){
		try {
			String loaction = "geo:0,0?q=" + cityDiscountDetailInfo.getLat() + "," + cityDiscountDetailInfo.getLng();
			Uri gmmIntentUri = Uri.parse(loaction);
			Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
			mapIntent.setPackage("com.google.android.apps.maps");
			activity.startActivity(mapIntent);

		} catch (Exception e) {
			Toast.makeText(activity, R.string.no_googglemap, Toast.LENGTH_SHORT).show();
		}
	}

	@OnClick(R.id.pro)
	public void onProClick() {
		if(StringUtil.isEmptyString(cityDiscountDetailInfo.getProLink())){
			return;
		}

		Intent intent = new Intent(activity,WebViewActivity.class);
		intent.putExtra("url",cityDiscountDetailInfo.getProLink());
		intent.putExtra("title",activity.getResources().getString(R.string.what_pro));
		activity.startActivity(intent);
	}

	@OnClick(R.id.detail)
	public void onDetailClick(View view) {
		if(!"Y".equals(cityDiscountDetailInfo.getPaid())){
			return;
		}

		if(StringUtil.isEmptyString(cityDiscountDetailInfo.getBusinessDescriptionHtml())){
			if(!StringUtil.isEmptyString(cityDiscountDetailInfo.getWebsite())){
				AppUtils.showBrower(activity, cityDiscountDetailInfo.getWebsite());
			}
			return;
		}
		Intent intent = new Intent(activity, WebViewActivity.class);
		intent.putExtra("html", cityDiscountDetailInfo.getBusinessDescriptionHtml());

		if (view != null && view.getId() == R.id.detail) {
			dragPosition = DraggerPosition.TOP;
		}
		intent.putExtra(WebViewActivity.DRAG_POSITION_EXTRA, dragPosition);
		activity.startActivity(intent);
	}

	public void subHandleMessage(Message msg) {
		switch (msg.what) {
			case Constants.BUSINESS_STATUS_CITYDISCOUNT_IMAGES_CHANGE:

				setUserImages(cityDiscountDetailInfo.getBusinessUserImages());
				break;
			case Constants.BUSINESS_STATUS_USER_COMMENT_UPVOTE:
			case Constants.BUSINESS_STATUS_USER_DELETE_COMMENT_UPVOTE:
				commentItemView.updateCommentLayout();
				break;
			}
	}

	public void taskMarkRefreshOver(ATaskMark taskMark) {
		if (taskMark.getTaskStatus() == ATaskMark.HANDLE_OVER) {
			rootView.refreshFinish(ARefreshViewController.SUCCEED);
		} else {
			rootView.refreshFinish(ARefreshViewController.FAIL);
		}
		cityDiscountDetailInfo = activity.getInfo();
		init();
	}

	@Override
	public void receiveResult(ATaskMark taskMark, ActionException exception, Object trackerResult) {
		if (taskMark instanceof UserRateTaskMark) {
			updateUserRateView();
		}
	}

	@Override
	public void onOverScrollUpdate(IOverScrollDecor decor, int state, float offset) {
		if (state == IOverScrollState.STATE_DRAG_START_SIDE || state == IOverScrollState.STATE_DRAG_END_SIDE) {
			if (Math.abs(offset) >= OVERSCROLL_THRESHOLD) {
				reachedOverScrollThreshold = true;
				scrollState = state;
			}
		}
	}

	@Override
	public void onOverScrollStateChange(IOverScrollDecor decor, int oldState, int newState) {
		if (oldState == IOverScrollState.STATE_BOUNCE_BACK && newState == IOverScrollState.STATE_IDLE && reachedOverScrollThreshold) {

			if (decor.getView() instanceof ViewPager) {
				if (scrollState == IOverScrollState.STATE_DRAG_START_SIDE) {
					dragPosition = DraggerPosition.LEFT;
					onDetailClick(null);
				} else if (scrollState == IOverScrollState.STATE_DRAG_END_SIDE) {
					dragPosition = DraggerPosition.RIGHT;
					onDetailClick(null);
				}
			} else if (decor.getView() instanceof ScrollView) {
				dragPosition = DraggerPosition.TOP;
				onDetailClick(null);
			}

			reachedOverScrollThreshold = false;
		}
	}

}
